package learn_effective_java.item_02.pizza;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;

/*
 * The builder pattern is well suited to class hierarchies.
 * 
 * Use a parallel hierarchy of builders, each nested in the corresponding class.
 * Abstract classes have abstract builders; 
 * concrete classes have concrete builders.
 * 
 * For example, consider an abstract class at the root of a hierarchy representing 
 * various kinds of pizza:
 */
public abstract class Pizza {
	public enum Topping {
		HAM, MUSHROOM, ONION, PEPPER, SAUSAGE
	}
	
	final Set<Topping> toppings;
	
	abstract static class Builder<T extends Builder<T>> {
		EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class);
		public T addTopping(Topping topping) {
			toppings.add(Objects.requireNonNull(topping));
			return self();
		}
		abstract Pizza build();
		protected abstract T self();
	}
	
	Pizza(Builder<?> builder) {
		toppings = builder.toppings.clone();
	}
}
